﻿using Ivi.Visa.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Test_Libscvs.Instrument
{
    public class Dmm_Set
    {
        public FormattedIO488 DMM_OI = null;
        public bool _enaSimulate = false; //用来表示仪表是否运行于offline模式下
        public string mode;
        public string Load_initial(string addr, bool simu, bool reset)
        {
            ResourceManager rm = new ResourceManager();
            DMM_OI = new FormattedIO488();
            string idn = null;
            try
            {
                DMM_OI = new Ivi.Visa.Interop.FormattedIO488();
                if (!simu)
                {
                    DMM_OI.IO = (IMessage)rm.Open(addr, AccessMode.NO_LOCK, 10000, "");
                    DMM_OI.IO.Timeout = 5000;
                    DMM_OI.WriteString("*IDN?", true); //Get the IDN string
                    idn = DMM_OI.ReadString();
                    if (reset)
                        DMM_OI.WriteString("*RST", true);   //Reset the device

                    return "DMM:" + idn;
                }
                else
                {
                    _enaSimulate = true;
                    return "Simulate";
                }
            }
            catch (Exception)
            {
                return "DMM Connect Fail"; //连接失败或者仿真的状态，分别返回不同的字符串
            }
        }
        public void close_session()
        {
            if (true)
            {
                DMM_OI.IO.Close();
            }
        }
        public double[] Dmm_Meas_IV()
        {
            double[] output = new double[2];
            DMM_OI.WriteString("MEAS:CURR:DC? 0.1");//Configure DC current measurements using the 1 A range. Then make and read one measurement
            Thread.Sleep(200);
            output[0] = Convert.ToDouble(DMM_OI.ReadString()) * 1000;
            DMM_OI.WriteString("MEAS:VOLT:DC? 100,0.00001");//Configure DC voltage measurements using the 10 V range with 1 mV resolution.Then make and read one measurement:
            Thread.Sleep(200);
            output[1] = Convert.ToDouble(DMM_OI.ReadString()) * 1000;
            return output;
        }
    }
}
